package com.tw101.module1;

public class Diamond {

    public static String diamond(int lines) {
        StringBuilder lineBuilder = new StringBuilder();
        int i, currentLines;
        for (i = 0, currentLines = 1; i < lines; i++, currentLines += 2) {
            if(i != 0) {
                lineBuilder.append("\n");
            }
            lineBuilder.append(String.format("%" + (lines - i + currentLines) + "s",
                    Triangle.horizontalLine(currentLines)));
        }
        for (i -= 2, currentLines -= 4; i >= 0; i--, currentLines -= 2) {
            lineBuilder.append("\n");
            lineBuilder.append(String.format("%" + (lines - i + currentLines) + "s",
                    Triangle.horizontalLine(currentLines)));
        }

        return lineBuilder.toString();
    }

    public static String diamondName(int lines, String name) {
        StringBuilder lineBuilder = new StringBuilder();
        int i, currentLines;
        for (i = 0, currentLines = 1; i < lines - 1; i++, currentLines += 2) {
            lineBuilder.append(String.format("%" + (lines - i + currentLines) + "s",
                    Triangle.horizontalLine(currentLines)));
            lineBuilder.append("\n");
        }
        lineBuilder.append(name);
        for (i -= 1, currentLines -= 2; i >= 0; i--, currentLines -= 2) {
            lineBuilder.append("\n");
            lineBuilder.append(String.format("%" + (lines - i + currentLines) + "s",
                    Triangle.horizontalLine(currentLines)));
        }

        return lineBuilder.toString();
    }

}
